package cn.training.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

//import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.training.bean.UserBean;
import cn.training.service.HelloWorldService;


//@Slf4j
@Controller("HelloWorldController")
@RequestMapping("/")
public class HelloWorldController {
	
	@Autowired
	HelloWorldService helloWorldService;
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model) {
        return "userlogin";
    }
    
    @RequestMapping(value = "/init", method = RequestMethod.POST)
    public String initLogin(UserBean userBean,Model model) {
    	UserBean result = helloWorldService.searchUser(userBean);
        model.addAttribute("userBean", result);
    	return "login";
    }
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public String list(UserBean userBean,Model model) {
    	List<UserBean> result = helloWorldService.listsearchUser(userBean);
        model.addAttribute("list", result);
    	return "userlist";
    }
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public String insert(UserBean userBean,Model model) {
    	if(helloWorldService.insertUser(userBean)!=0)
    		return "success";
    	else
    		return "helloWorld";
    }
    
    @RequestMapping(value = "/supdate", method = RequestMethod.POST)
    public String supdate(UserBean userBean,Model model) {
    	UserBean result = helloWorldService.supdateUser(userBean);
        model.addAttribute("userBean", result);
    	return "login";
    }
    
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(UserBean userBean,Model model) {
    	if(helloWorldService.updateUser(userBean)!=0)
    		return "success";
    	else
    		return "helloWorld";
    }
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(UserBean userBean,Model model) {
    	int delete = helloWorldService.deleteUser(userBean);
    	model.addAttribute("list", delete);
    	List<UserBean> result = helloWorldService.listsearchUser(userBean);
        model.addAttribute("list", result);
    	return "userlist";
    }
    
//用户登录   
    @RequestMapping(value = "/userLogin", method = RequestMethod.POST, params="login")
    public String login(UserBean userBean,Model model) {
    	UserBean res = helloWorldService.searchUser(userBean);    	
    	if(res!=null)
    	{
    		List<UserBean> result = helloWorldService.searchAll(userBean);
    		model.addAttribute("userBean", result);       
        	return "index";
    	}   		
        else
        {
        	model.addAttribute("userlogin", "用户名或密码错误！");
        	return "userlogin";
        }
        	
    }
    
    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public String index(UserBean userBean,Model model){
    	List<UserBean> result = helloWorldService.listsearchUser(userBean);
        model.addAttribute("userBean", result);
    	return "index";
    }
    
    @RequestMapping(value = "/userLogin",method = RequestMethod.POST, params="reg")
    public String reg(UserBean userBean,Model model){
    	return "register";
    }

//用户注册
    @RequestMapping(value = "/register", method = RequestMethod.POST,params="userregister")
    public String register(HttpServletRequest req,UserBean userBean,Model model) {
    	String password = req.getParameter("password");
    	String repassword = req.getParameter("repassword");
    	if(password.equals(repassword))
    	{
    		if(helloWorldService.registerUser(userBean)!=0)
        		return "userlogin";
        	else
        		return "register";
    	}
    	else
    	{
    		model.addAttribute("register", "密码和确认密码不一致！");
    		return "register";
    	}
    		
    }
    
    @RequestMapping(value = "/register",method = RequestMethod.POST,params="backlogin")
    public String backlogin(UserBean userBean,Model model){
    	return "userlogin";
    }
 
//删除用户
    @RequestMapping(value = "/deleteu",method = RequestMethod.POST,params="deleten")
    public String back(UserBean userBean,Model model){
    	List<UserBean> result = helloWorldService.listsearchUser(userBean);
        model.addAttribute("userBean", result);
    	return "index";
    }

    @RequestMapping(value = "/delete1",method = RequestMethod.GET)
    public String delete1(UserBean userBean,Model model){
    	UserBean result = helloWorldService.okdeleteUser(userBean);
        model.addAttribute("userBean", result);
    	return "confirm";
    }
    
    @RequestMapping(value = "/deleteu", method = RequestMethod.POST,params="deletey")
    public String deleteu(UserBean userBean,Model model) {
    	int deleteu = helloWorldService.deleteUser(userBean);
    	model.addAttribute("userBean", deleteu);
    	List<UserBean> result = helloWorldService.listsearchUser(userBean);
        model.addAttribute("userBean", result);
    	return "index";
    }

//更新用户信息
    @RequestMapping(value = "/update1",method = RequestMethod.GET)
    public String update1(UserBean userBean,Model model){
    	UserBean result = helloWorldService.update1User(userBean);
        model.addAttribute("userBean", result);
    	return "update";
    }

    @RequestMapping(value = "/userupdate", method = RequestMethod.POST)
    public String userupdate(HttpServletRequest req,Model model,@Valid @ModelAttribute("userBean") UserBean userBean, BindingResult results) {
//    	String password = req.getParameter("password");
//    	String repassword = req.getParameter("repassword");
    	
//判断用户名密码不能为空
    	if(results.hasErrors())
    	{          
    		model.addAttribute("update", "用户名密码不能为空");
    		return "update";
    	}


//    	if(userBean.getUsername().isEmpty()||userBean.getPassword().isEmpty())
//    	{
//    		UserBean result = helloWorldService.update1User(userBean);
////    		log.info("用户名或密码不能为空");
//    		model.addAttribute("userBean", result);
//    		model.addAttribute("update", "用户名或密码不能为空，请输入！");
//			return "update";
//    	}
    	
//判断密码与确认密码是否一致   	
    	if(userBean.getPassword().equals(userBean.getRepassword()))
    	{
	    	if(helloWorldService.userupdateUser(userBean)!=0)
	    	{
	    		List<UserBean> result = helloWorldService.listsearchUser(userBean);
		        model.addAttribute("userBean", result);
		    	return "index";
	    	}
	    		
	    	else
	    	{
	    		model.addAttribute("update", "用户名密码不能为空，请输入！");
	    		return "update";
	    	}
    	}
    	else
    	{
    		UserBean result = helloWorldService.update1User(userBean);
    		model.addAttribute("userBean", result);
    		model.addAttribute("update", "密码和确认密码不一致！");
    		return "update";
    	}
    		
    }
}
